const UnionFind = require("../template")

function testUnionFindAdvancedOperations() {
  const uf = new UnionFind(6)

  console.log(
    "1. 路径压缩测试:",
    (() => {
      uf.union(0, 1)
      uf.union(1, 2)
      uf.union(2, 3)
      const root1 = uf.find(0)
      const root2 = uf.find(3)
      return root1 === root2 && uf.connected(0, 3) === true
    })()
  )

  console.log(
    "2. 按秩合并测试:",
    (() => {
      const rankUf = new UnionFind(4)
      rankUf.union(0, 1)
      rankUf.union(2, 3)
      rankUf.union(0, 2)
      return rankUf.getComponentsCount() === 1 &&
             rankUf.getComponentSize(0) === 4
    })()
  )

  console.log(
    "3. 获取所有连通分量测试:",
    (() => {
      const componentsUf = new UnionFind(5)
      componentsUf.union(0, 1)
      componentsUf.union(2, 3)
      const components = componentsUf.getAllComponents()
      return components.length === 3 && // [0,1], [2,3], [4]
             components.some(comp => comp.length === 2) &&
             components.some(comp => comp.length === 1)
    })()
  )

  console.log(
    "4. 最大连通分量测试:",
    (() => {
      const maxUf = new UnionFind(6)
      maxUf.union(0, 1)
      maxUf.union(2, 3)
      maxUf.union(4, 5)
      maxUf.union(0, 2)
      return maxUf.getLargestComponentSize() === 4
    })()
  )

  console.log(
    "5. 重置并查集测试:",
    (() => {
      uf.reset()
      return uf.getComponentsCount() === 6 &&
             uf.getComponentSize(0) === 1 &&
             uf.connected(0, 1) === false
    })()
  )

  console.log(
    "6. 添加新元素测试:",
    (() => {
      const newIndex = uf.addElement()
      return newIndex === 6 &&
             uf.getComponentsCount() === 7 &&
             uf.getComponentSize(6) === 1
    })()
  )

  console.log(
    "7. this上下文高级操作测试:",
    (() => {
      const ufObj = {
        multiplier: 2,
        processAdvanced: function(n) {
          const uf = new UnionFind(n)
          uf.union(0, 1)
          uf.union(2, 3)
          return uf.getComponentsCount() * this.multiplier
        }
      }
      return ufObj.processAdvanced(4) === 4 // (4-2)*2=4
    })()
  )

  console.log(
    "8. 复杂对象this绑定高级操作测试:",
    (() => {
      const ufConfigObj = {
        config: { threshold: 3 },
        processWithConfig: function(n) {
          const uf = new UnionFind(n)
          uf.union(0, 1)
          uf.union(2, 3)
          return uf.getLargestComponentSize() >= this.config.threshold
        }
      }
      return ufConfigObj.processWithConfig(4) === false
    })()
  )

  console.log(
    "9. 高级操作一致性测试:",
    (() => {
      const consistencyUf = new UnionFind(5)
      consistencyUf.union(0, 1)
      consistencyUf.union(2, 3)
      const count1 = consistencyUf.getComponentsCount()
      const size1 = consistencyUf.getLargestComponentSize()
      consistencyUf.union(1, 2)
      const count2 = consistencyUf.getComponentsCount()
      const size2 = consistencyUf.getLargestComponentSize()
      return count1 === 3 && size1 === 2 && count2 === 2 && size2 === 4
    })()
  )

  console.log(
    "10. 高级操作边界测试:",
    (() => {
      const boundaryUf = new UnionFind(1)
      const components = boundaryUf.getAllComponents()
      const maxSize = boundaryUf.getLargestComponentSize()
      boundaryUf.addElement()
      const newCount = boundaryUf.getComponentsCount()
      return components.length === 1 && maxSize === 1 && newCount === 2
    })()
  )

  console.log(
    "11. 高级操作性能测试:",
    (() => {
      const perfUf = new UnionFind(1000)
      const start = performance.now()
      for (let i = 0; i < 1000; i += 2) {
        perfUf.union(i, i + 1)
      }
      for (let i = 0; i < 1000; i += 4) {
        perfUf.union(i, i + 2)
      }
      const end = performance.now()
      return perfUf.getComponentsCount() === 250 && (end - start) < 1000
    })()
  )

  console.log(
    "12. 高级操作复杂测试:",
    (() => {
      const complexUf = new UnionFind(8)
      complexUf.union(0, 1)
      complexUf.union(2, 3)
      complexUf.union(4, 5)
      complexUf.union(6, 7)
      complexUf.union(0, 2)
      complexUf.union(4, 6)
      complexUf.union(0, 4)
      const components = complexUf.getAllComponents()
      const maxSize = complexUf.getLargestComponentSize()
      return components.length === 1 && maxSize === 8
    })()
  )
}

testUnionFindAdvancedOperations()
